﻿using _02_Prism_Test.Views;
using Prism.Commands;
using Prism.DryIoc;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;

namespace _02_Prism_Test.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public DelegateCommand<String> ShowContentCmd { get; set; }
        public DelegateCommand BackCmd { get; set; }
        public DelegateCommand<string> ShowDialogCmd { get; set; }  //对话框命令

        public DelegateCommand<string> PubEventCmd { get; set; }  //发布事件命令
        public DelegateCommand<string> SubEventCmd { get; set; }  //订阅事件命令
        public DelegateCommand<string> UnSubEventCmd { get; set; }  //订阅事件命令

        private IRegionNavigationJournal Journal;   //导航记录器

        private IDialogService DialogService;  //对话框服务

        readonly IRegionManager RegionManager;  //区域管理器

        private readonly IEventAggregator EventAggregator;  //事件聚合器

        public MainWindowViewModel(IRegionManager _regionManager, IDialogService _dialogService, IEventAggregator _eventAggregator)
        {
            RegionManager = _regionManager;
            DialogService = _dialogService;
            EventAggregator = _eventAggregator;
            //ShowContentCmd = new DelegateCommand<string>(ShowContent);  //未实现view与viewmodel的解耦，
            ShowContentCmd = new DelegateCommand<string>(ShowContent_Prism_QY);  //通过prism的MVVM框架实现view与viewmodel的解耦，这里采用区域注入的方式
            BackCmd = new DelegateCommand(Back);
            ShowDialogCmd = new DelegateCommand<string>(ShowDialog);

            SubEventCmd = new DelegateCommand<string>(SubEvent);
            PubEventCmd = new DelegateCommand<string>(PubEvent);
            UnSubEventCmd = new DelegateCommand<string>(UnSubEvent);
        }

        private void UnSubEvent(string obj)
        {
            EventAggregator.GetEvent<MsgEvent>().Unsubscribe(Sub);  //取消订阅事件
        }

        /// <summary>
        /// 发布的信息
        /// </summary>
        /// <param name="obj"></param>
        private void PubEvent(string obj)
        {
            EventAggregator.GetEvent<MsgEvent>().Publish("我想去旅游了！");  //发布事件
        }

        /// <summary>
        /// 接收到的订阅信息
        /// </summary>
        /// <param name="obj"></param>
        private void SubEvent(string obj)
        {
            //EventAggregator.GetEvent<MsgEvent>().Subscribe((msg) =>
            //{
            //    MessageBox.Show($"我收到的信息：{msg}");
            //}); 
            EventAggregator.GetEvent<MsgEvent>().Subscribe(Sub);
        }

        private void Sub(string obj)
        {
            MessageBox.Show($"我收到的信息：{obj}");
        }

        private void ShowDialog(string ViewName)
        {
            DialogParameters paras= new DialogParameters();
            paras.Add("Title", "动态传递的标题");        //键值对，字典
            paras.Add("p1", "业务参数值1");        //键值对，字典
            paras.Add("p2", "业务参数值2");        //键值对，字典
            DialogService.ShowDialog(ViewName,paras, callback => 
            {
                if (callback.Result == ButtonResult.Yes)
                { 
                    string r1 = callback.Parameters.GetValue<string>("MsgA");
                }

                else if (callback.Result == ButtonResult.No)
                { 
                
                }

                else if(callback.Result == ButtonResult.None)
                { 
                
                }
            });
        }

        private void Back()
        {
            if(Journal.CanGoBack && Journal!=null)
                Journal.GoBack();
        }

        ///// <summary>
        ///// 未实现view与viewmodel的解耦，直接在viewmodel中实例化view
        ///// </summary>
        ///// <param name="ViewName"></param>
        //private void ShowContent(string ViewName)
        //{
        //    if (ViewName == "ViewA")
        //        UserContent = new ViewA();
        //    else if (ViewName == "ViewB")
        //        UserContent = new ViewB();
        //}



        /// <summary>
        /// 通过prism的MVVM框架实现view与viewmodel的解耦，这里采用区域注入的方式
        /// </summary>
        /// <param name="ViewName"></param>
        private void ShowContent_Prism_QY(string ViewName)  
        {
            //RegionManager.RequestNavigate("ContentRegion", ViewName);  //ContentRegion是MainWindow.xaml中定义的区域名称
            NavigationParameters parameters = new NavigationParameters();
            parameters.Add("MsgA", "大家好，我是A，传参");        //键值对，字典
            RegionManager.Regions["ContentRegion"].RequestNavigate(ViewName, callback => 
            {
                Journal = callback.Context.NavigationService.Journal;
            },parameters);  //ContentRegion是MainWindow.xaml中定义的区域名称
        }

        private UserControl _UserContent;

        public UserControl UserContent
        {
            get { return _UserContent; }
            set
            {
                _UserContent = value;
                RaisePropertyChanged("UserContent");
            }
        }

    }
}
